package com.sl.aoptest

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.sl.permission.annotation.AopPermission
import com.sl.permission.annotation.PermissionDenied
import com.sl.permission.annotation.PermissionDeniedForever
import com.sl.statistical.annotation.MaiDianData
import com.sl.statistical.annotation.ParamsAnnotation

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun testAspetj(view: View) {
        getTime()
        getTime("")
    }


    @ExecuteTime
    fun getTime() {
        try {
            Thread.sleep(3000)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @ExecuteTime
    fun getTime(s: String) {
        try {
            Thread.sleep(3000)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    //获取权限
    fun getSd(view: View) {
        realDo()
    }

    @AopPermission(
        [Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE],
        requestCode = 1
    )
    fun realDo() {
        Toast.makeText(this, "用户同意了存储权限", Toast.LENGTH_SHORT).show()
    }

    @PermissionDenied(1)
    fun permissionCancel() {
        Toast.makeText(this, "用户拒绝了存储权限", Toast.LENGTH_SHORT).show()
    }

    @PermissionDeniedForever(1)
    fun permissionDenied() {
        Toast.makeText(this, "用户永久拒绝了存储权限", Toast.LENGTH_SHORT).show()
    }

    fun goSetting(view: View) {
        val uri = Uri.parse("package:$packageName")
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri)
        startActivity(intent)
    }


    fun getRecord(view: View) {
        getRecord()
    }

    @AopPermission([Manifest.permission.RECORD_AUDIO], requestCode = 2)
    fun getRecord() {
        Toast.makeText(this, "用户同意了录音权限", Toast.LENGTH_SHORT).show()
    }

    @PermissionDenied(2)
    fun permissionRecordCancel() {
        Toast.makeText(this, "用户拒绝了录音权限", Toast.LENGTH_SHORT).show()
    }

    @PermissionDeniedForever(2)
    fun permissionRecordDeniedForever() {
        Toast.makeText(this, "用户永久拒绝了录音权限", Toast.LENGTH_SHORT).show()
    }

    fun maiDianUpload(view: View) {
        mainDianUpload("张三","123456")
    }

    @MaiDianData
    private fun mainDianUpload(
        @ParamsAnnotation("name") name: String,
        @ParamsAnnotation("nick") value: String
    ) {
        Toast.makeText(this, "埋点执行", Toast.LENGTH_SHORT).show()
    }


    //    fun initPremission(){
//        //检查权限，没有权限
//        if (ContextCompat.checkSelfPermission(this,Manifest.permission_group.STORAGE) != PackageManager.PERMISSION_GRANTED){
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//                //判断权限是否被永久拒绝
//                if (!shouldShowRequestPermissionRationale(Manifest.permission_group.STORAGE)){
//                    //跳转设置页
//                    val packageUrI : Uri = Uri.parse("package:com.sl.aoptest")
//                    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,packageUrI)
//                    startActivity(intent)
//                }else{
//                    //请求权限
//                    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission_group.STORAGE),1)
//                }
//            }else{
//
//            }
//        }else{
//            //有权限
//            Toast.makeText(this,"有权限",Toast.LENGTH_SHORT).show()
//        }
//    }

//    override fun onRequestPermissionsResult(
//        requestCode: Int,
//        permissions: Array<out String>,
//        grantResults: IntArray
//    ) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//       when(requestCode){
//           1 ->  if ( grantResults.isNotEmpty()) {
//                run m@{
//                    grantResults.forEach {
//                        if (it != PackageManager.PERMISSION_DENIED) {
//                            Toast.makeText(this, "未拥有相关权限", Toast.LENGTH_SHORT).show()
//                            return@m
//                        }
//                    }
//                }
//            }else{
//                Toast.makeText(this, "未拥有相关权限", Toast.LENGTH_SHORT).show()
//            }
//        }
//    }

}